﻿using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YuRadio
{
    public class Frm_curd
    {

        Activity Context;
        private LinearLayout thisLayout;
        private Button btnCancel;
        private Button btnSave;
        private EditText editName;
        private EditText editUrl;
        private EditText editIcon;

        int AddOrEdit = 1;

        public event EventHandler<AddStationEventArgs> OnChanged;
        public static Frm_curd Init(Activity context,int id)
        {
            var frm =new Frm_curd();
            frm.Context = context;
            frm.initComponents();
            frm.thisLayout = context.FindViewById<LinearLayout>(id);
            return frm;
        }

        public void initComponents()
        {
            btnCancel =Context.FindViewById<Button>(Resource.Id.edst_btn_cancel);
            btnSave = Context.FindViewById<Button>(Resource.Id.edst_btn_save);
            editName = Context.FindViewById<EditText>(Resource.Id.edst_name);
            editUrl = Context.FindViewById<EditText>(Resource.Id.edst_url);
            editIcon = Context.FindViewById<EditText>(Resource.Id.edst_icon);

            btnCancel.Click += BtnCancel_Click;
            btnSave.Click += BtnSave_Click;
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            string err = "";
            if (validated(ref err,out Station station))
            {
                thisLayout.Visibility = ViewStates.Invisible;
                if (OnChanged != null)
                {
                    OnChanged(this, new AddStationEventArgs { Station = station,AddorEdit=AddOrEdit });
                }
            }
            else
            {
                Toast.MakeText(Context, "请输入正确的数据 " + err, ToastLength.Long).Show();
            }
        }

        private bool validated(ref string err,out Station station)
        {
            bool result = true;
            station = new Station { Name = editName.Text.Trim(), Icon = editIcon.Text.Trim(), Url = editUrl.Text.Trim() };
            if (station.Name.Length<2 || station.Name.Length>20)
            {
                err = "请给 Name 一个正确的值，最少要 2 个字,但不能超过 20 个字";
                result = false;
            }
            if (station.Url.Length < 5 || station.Url.Length > 100)
            {
                err = "请给 Name 一个正确的值，最少要 5 个字,但不能超过 100 个字";
                result = false;
            }
            station.Icon = "";

            return result;
        }

        internal void AddNew()
        {
            clearEdits();
            this.AddOrEdit = 1;
            thisLayout.Visibility = ViewStates.Visible;
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            thisLayout.Visibility = ViewStates.Invisible;
        }

        internal void Edit(Station station)
        {
            this.editName.SetText(station.Name, TextView.BufferType.Editable);
            this.editUrl.SetText(station.Url, TextView.BufferType.Editable);
            this.editIcon.SetText(station.Icon, TextView.BufferType.Editable);
            this.AddOrEdit = 0;
            thisLayout.Visibility = ViewStates.Visible;
        }

        

        private void clearEdits()
        {
            this.editName.SetText("", TextView.BufferType.Editable);
            this.editUrl.SetText("", TextView.BufferType.Editable);
            this.editIcon.SetText("", TextView.BufferType.Editable);
        }
    }

    public class AddStationEventArgs : EventArgs
    {
        public Station Station { get; set; }
        public int AddorEdit { get; set; }
    }
}